package api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"github.com/goccy/go-json"
	"go-caipu/pkg/api/helper"
	"go-caipu/pkg/constant"
	"go-caipu/pkg/services/admin/sysapi"
	"go-caipu/pkg/util"
	"time"
)

func (hs *HTTPServer) GetSysApiPage(c *gin.Context) {
	cmd := sysapi.GetSysApiPageCommand{}
	if err := helper.BindAndValidate(c, &cmd, binding.Form); err != nil {
		return
	}
	result, count, err := hs.apiService.GetSysApiPage(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	if result == nil {
		result = []sysapi.SysApi{}
	}
	helper.SuccessWithData(c, helper.Page{CurrentPage: cmd.PageIndex, PageSize: cmd.PageSize, List: result, Total: count})
}
func (hs *HTTPServer) GetSysApi(c *gin.Context) {
	cmd := sysapi.GetSysApiCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	result, err := hs.apiService.Get(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}
func (hs *HTTPServer) CreateSysApi(c *gin.Context) {
	cmd := sysapi.SysApi{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	cmd.Id = hs.Snowflake.Generate().Int64()
	err := hs.apiService.Insert(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) UpdateSysApi(c *gin.Context) {
	cmd := sysapi.SysApi{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		return
	}
	err := hs.apiService.Update(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

func (hs *HTTPServer) RemoveSysApi(c *gin.Context) {
	cmd := sysapi.RemoveSysApiCommand{}
	//if err := helper.BindAndValidate(c, &cmd, binding.JSON); err != nil {
	//	return
	//}
	cmd.Id = util.StringToInt64(c.Param("id"))
	if cmd.Id > 0 {
		cmd.Ids = []int64{cmd.Id}
	}
	err := hs.apiService.Remove(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}

// ExportSysApi 导出接口信息数据
func (hs *HTTPServer) ExportSysApi(c *gin.Context) {
	cmd := sysapi.GetApiCategoriesCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	result, err := hs.apiService.ExportSysApiCategory(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	if len(result) == 0 {
		c.Writer.Write([]byte("未找到符合的api接口记录"))
	}
	c.Header("Content-Disposition", "attachment; filename="+fmt.Sprintf("api-%s.json", time.Now().Format("20060102150405")))

	jsonData, _ := json.Marshal(result)
	c.Writer.Write(jsonData)
}
func (hs *HTTPServer) GetSysApiCategory(c *gin.Context) {
	isApi := c.Query("isApi") == "true"
	result, err := hs.apiService.GetSysApiCategoryTree(c.Request.Context(), sysapi.GetApiCategoriesCommand{IsApi: isApi})
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithData(c, result)
}
func (hs *HTTPServer) CreateSysApiCategory(c *gin.Context) {
	cmd := sysapi.ApiCategory{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	cmd.ID = hs.Snowflake.Generate().Int64() //生成主键编号
	cmd.OperUser = hs.getJwtUserID(c)
	err := hs.apiService.InsertApiCategory(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}
func (hs *HTTPServer) UpdateSysApiCategory(c *gin.Context) {
	cmd := sysapi.ApiCategory{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	cmd.OperUser = hs.getJwtUserID(c)
	err := hs.apiService.UpdateApiCategory(c.Request.Context(), cmd)
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}
func (hs *HTTPServer) RemoveSysApiCategory(c *gin.Context) {
	cmd := sysapi.RemoveSysApiCategoryCommand{}
	if err := helper.BindAndValidate(c, &cmd); err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	err := hs.apiService.RemoveApiCategory(c.Request.Context(), sysapi.ApiCategory{ID: cmd.Id})
	if err != nil {
		helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
		return
	}
	helper.SuccessWithOutData(c)
}
